package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.FileReportListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.FileReport;
import com.isunland.managesystem.entity.FileReportOriginal;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Lenovo on 2017/2/28.
 */
public class FileReportListFragment extends BaseListFragment {
  protected static final String EXTRA_MAINID =
      "com.isunland.managesystem.ui.FileReportListFragment.EXTRA_MAINID";
  private static final int REQUEST_DETAIL = 0;
  private ArrayList<FileReport> mList;
  private String mainId;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mainId = getArguments().getString(EXTRA_MAINID);
    mList = new ArrayList<>();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        return true;
      case R.id.menu_item_add:
        Intent intent = new Intent(getActivity(), FileReportDetailActivity.class);
        intent.putExtra(FileReportDetailFragment.EXTRA_MAINID, mainId);
        startActivityForResult(intent, REQUEST_DETAIL);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("mainid", mainId);
    return params;
  }

  @Override public String getUrl() {
    return ApiConst.URL_FILE_REPORT_LIST;
  }

  @Override public void resolveResponse(String json) {
    FileReportOriginal original = new Gson().fromJson(json, FileReportOriginal.class);
    if (original.getResult() == Base.SUCCESS && original.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(original.getRows());
      FileReportListAdapter mAdapter = new FileReportListAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    FileReport content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), FileReportDetailActivity.class);
    intent.putExtra(FileReportDetailFragment.EXTRA_CONTENT, content);
    intent.putExtra(FileReportDetailFragment.EXTRA_MAINID, mainId);
    startActivityForResult(intent, REQUEST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DETAIL) {
      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public static FileReportListFragment newInstance(String mainId) {

    Bundle args = new Bundle();
    args.putString(EXTRA_MAINID, mainId);
    FileReportListFragment fragment = new FileReportListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
